@include('_include.header')
<style>
    .layui-tab-title {
        left: 25px;
    }
</style>

<body>

@include('_include.plugins_title')

<div class="layui-tab" lay-filter="demo">
    <ul class="layui-tab-title">
        <li class="layui-this">助力商品</li>
        <li>助力订单<span class="layui-badge">{{$order_count}}</span></li>
    </ul>
    <div class="layui-tab-content">
        <div class="layui-tab-item layui-show">
            <div class="layui-fluid">
                <div class="larry-container">
                    <div class="layui-row layui-col-space15 larryms-data-top">
                        <div class="layui-col-xs12 layui-col-sm12 layui-col-md12 layui-col-lg12">
                            <script type="text/html" id="tools">
                                <button type="button" class="layui-btn" lay-event="add">新增</button>
                            </script>
                            <table lay-filter="ad_table" class="layui-table"
                                   lay-data="{height:'full-155',cellMinWidth:95,url:'{{url('plugins/share-free/index')}}', page:true, id:'ad_table',toolbar:'#tools',defaultToolbar:[]}">
                                <thead>
                                <tr>
                                    <th lay-data="{field:'id', align:'center'}">ID</th>
                                    <th lay-data="{field:'goods_name',align:'center'}">助力商品</th>
                                    <th lay-data="{toolbar:'#toolbarDemo',width:200,align:'center'}">商品主图</th>
                                    <th lay-data="{field:'amount',align:'center',edit:'text'}">金额</th>
                                    <th lay-data="{field:'numbers',align:'center',edit:'text'}">需要邀请人数</th>
                                    <th lay-data="{field:'sort',align:'center',edit:'text'}">排序</th>
                                    <th lay-data="{field:'days',align:'center',edit:'text'}">邀请有效天数</th>
                                    <th lay-data="{field:'part_numbers',align:'center',edit:'text'}">虚假参与人数(万)</th>
                                    <th lay-data="{field:'create_at',align:'center'}">创建时间</th>
                                    <th lay-data="{title:'操作',templet:'#goodsListBar',align:'center'}">操作</th>
                                </tr>
                                </thead>
                            </table>
                        </div>
                    </div>
                </div>
            </div>

        </div>
        <div class="layui-tab-item">

            <div class="layui-fluid">
                <div class="larry-container">
                    <div class="layui-row layui-col-space15 larryms-data-top">
                        <div >
                            <table lay-filter="order_table" class="layui-table"
                                   lay-data="{height:'full-155',cellMinWidth:95,url:'{{url('plugins/share-free/order')}}', page:true, id:'order_table',defaultToolbar:[]}">
                                <thead>
                                <tr>
                                    <th lay-data="{field:'id', align:'center'}">ID</th>
                                    <th lay-data="{field:'goods_name',align:'center'}">商品</th>
                                    <th lay-data="{toolbar:'#toolbarUser',width:160,align:'center'}">用户</th>
                                    <th lay-data="{toolbar:'#toolbarDemo',width:200,align:'center'}">商品主图</th>
                                    <th lay-data="{field:'amount',align:'center'}">金额</th>
                                    <th lay-data="{field:'numbers',align:'center'}">邀请总人数</th>
                                    <th lay-data="{field:'now_numbers',align:'center'}">当前邀请人数</th>
                                    <th lay-data="{field:'status_string',align:'center'}">状态</th>
                                    <th lay-data="{field:'end_time',align:'center'}">结束时间</th>
                                    <th lay-data="{field:'create_at',align:'center'}">创建时间</th>
                                    <th lay-data="{title:'操作',templet:'#listBar',align:'center'}">操作</th>
                                </tr>
                                </thead>
                            </table>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>
</div>


<script type="text/html" id="toolbarDemo" class="layer-photos-demo">
    <img src="@{{ d.goods_image }}" style="width: 30px;height: 30px" layer-src="@{{ d.goods_image }}" alt=""
         lay-event="show">
</script>

<script type="text/html" id="toolbarUser" class="layer-photos-demo">
    <img src="@{{ d.user.avatar }}" width="30px" height="30px" layer-src="@{{ d.user.avatar }}" style="border-radius: 100%" alt="" lay-event="show">
    <span style="margin-left: 10px">@{{d.user.nick_name}}</span>
</script>


<script type="text/html" id="goodsListBar">
{{--    <a class="layui-btn layui-btn-xs" data-url="{{url('plugins/share-free/edit-goods')}}?id=@{{ d.id }}"--}}
{{--       lay-event="edit">编辑</a>--}}
    <a class="layui-btn layui-btn-danger layui-btn-xs" data-url="{{url('plugins/share-free/delete-goods')}}?id=@{{ d.id }}"
       lay-event="delete">删除</a>
</script>

<script type="text/html" id="listBar">
    <a class="layui-btn layui-btn-xs" data-url="{{url('plugins/share-free/log')}}/@{{ d.id }}" lay-event="log">查看邀请人</a>
{{--    <a class="layui-btn layui-btn-danger layui-btn-xs" data-url="{{url('plugins/share-free/order-delete')}}?id=@{{ d.id }}"--}}
{{--       lay-event="delete">订单作废</a>--}}
</script>

<script>
    layui.config({
        base: "/plugin/layuiadmin/"  //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table'], function () {
        var $ = layui.$,
            form = layui.form,
            table = layui.table;

        table.on('toolbar(ad_table)', function (obj) {
            var that = this;
            if (obj.event == 'add') {
                var index = layer.open({
                    title: "添加商品",
                    type: 2,
                    area: ['760px', '850px'],
                    scrollbar: true,
                    content: "{{url('plugins/share-free/edit-goods')}}",
                    end: function () {
                        $(that).removeAttr("data-flag");
                        layui.cache.layerIndex = null;
                        table.reload('ad_table')
                    }
                });
                // layer.full(index);
            }
        });

        table.on('edit(ad_table)', function (obj) {

            var url = "{{url('plugins/share-free/edit-goods')}}";

            var data = {};
            data[obj.field] = obj.value;
            data['id'] = obj.data.id;

            $.ajax({
                url: url,
                dateType: 'json',
                data: data,
                beforeSend: function () {
                },
                type: 'post',
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                success: function (res) {

                    if (res.status === SUCCESS) {
                        layer.msg("操作成功", {time: 800, shade: 0.3}, function () {
                            table.reload('ad_table')
                        });
                    } else {
                        layer.msg(res.message)
                    }

                }
            })
        });

        table.on('tool(order_table)', function (obj) {
            var data = obj.data;
            var that = this;
            var url = $(this).data('url');
            if(obj.event === 'log'){
                layer.open({
                    title: '邀请记录',
                    type: 2,
                    area: ['760px', '850px'],
                    content: url,
                    end: function () {
                        $(that).removeAttr("data-flag");
                        layui.cache.layerIndex = null;
                        table.reload('ad_table')
                    }
                });
            }
        })

        table.on('tool(ad_table)', function (obj) {
            var data = obj.data;
            var that = this;
            var url = $(this).data('url');

            switch (obj.event) {
                case "show":
                    var arr = [];
                    var obj = {
                        'alt': data.goods_image,
                        'pic': data.id + "_id",
                        'src': data.goods_image,
                        'thumb': data.goods_image
                    };
                    arr.push(obj);
                    var json = {
                        'title': data.goods_image,
                        'id': data.id,
                        'start': 0,
                        'data': arr
                    };
                    layer.photos({
                        photos: json
                        , anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
                    });
                    break;



                case 'delete':
                    layer.confirm('是否确认删除', {
                        btn: ['取消', '确认'], //按钮
                        title: '温馨提示'
                    }, function () {
                        layer.closeAll();
                    }, function () {
                        $.ajax({
                            url: url,
                            dateType: 'json',
                            type: 'post',
                            headers: {
                                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                            },
                            success: function (res) {
                                layer.closeAll();
                                layer.msg(res.message);
                                table.reload('ad_table')
                            }
                        });

                    });
                    break;

                default:
                    break;

            }

        });

    });

</script>
</body>
@include('_include.footer')